package com.drug.wms.service.impl;

import com.drug.common.utils.DateUtils;
import com.drug.common.utils.uuid.UUID;
import com.drug.wms.domain.BusSaleGoodsInfo;
import com.drug.wms.domain.BusSalesSlip;
import com.drug.wms.domain.BusSkuStock;
import com.drug.wms.mapper.BusSaleGoodsInfoMapper;
import com.drug.wms.mapper.BusSalesSlipMapper;
import com.drug.wms.page.BusSalesSlipPage;
import com.drug.wms.service.GenerateSequence;
import com.drug.wms.service.IBusSalesSlipService;
import com.drug.wms.service.IBusSkuStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品销售单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@Service
public class BusSalesSlipServiceImpl implements IBusSalesSlipService {
    @Resource
    private BusSalesSlipMapper busSalesSlipMapper;
    @Resource
    private BusSaleGoodsInfoMapper busSaleGoodsInfoMapper;

    /**
     * 查询商品销售单
     *
     * @param id 商品销售单主键
     * @return 商品销售单
     */
    @Override
    public BusSalesSlipPage selectBusSalesSlipById(String id) {
        BusSalesSlip busSalesSlip = busSalesSlipMapper.selectBusSalesSlipById(id);
        BusSaleGoodsInfo busSaleGoodsInfo = new BusSaleGoodsInfo();
        busSaleGoodsInfo.setSalesId(id);
        List<BusSaleGoodsInfo> busSaleGoodsInfos = busSaleGoodsInfoMapper.selectBusSaleGoodsInfoList(busSaleGoodsInfo);
        BusSalesSlipPage busSalesSlipPage = new BusSalesSlipPage();
        busSalesSlipPage.setBusSalesSlip(busSalesSlip);
        busSalesSlipPage.setBusSaleGoodsInfoList(busSaleGoodsInfos);
        return busSalesSlipPage;
    }

    /**
     * 查询商品销售单列表
     *
     * @param busSalesSlip 商品销售单
     * @return 商品销售单
     */
    @Override
    public List<BusSalesSlip> selectBusSalesSlipList(BusSalesSlip busSalesSlip) {
        return busSalesSlipMapper.selectBusSalesSlipList(busSalesSlip);
    }

    /**
     * 新增商品销售单
     *
     * @param busSalesSlipPage 商品销售单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusSalesSlip(BusSalesSlipPage busSalesSlipPage) {
        BusSalesSlip busSalesSlip = busSalesSlipPage.getBusSalesSlip();
        busSalesSlip.setId(null);
        busSalesSlip.setCreateTime(DateUtils.getNowDate());

        busSalesSlip.setSaleNo(GenerateSequence.genSeqNo("XS", 10));
        busSalesSlipMapper.insertBusSalesSlip(busSalesSlip);
        //出库库存
        List<BusSkuStock> busSkuStockList = new ArrayList<>();
        List<BusSaleGoodsInfo> busSaleGoodsInfoList = busSalesSlipPage.getBusSaleGoodsInfoList();
        for (BusSaleGoodsInfo b : busSaleGoodsInfoList) {
            b.setSalesId(busSalesSlip.getId());
            b.setId(null);
            busSaleGoodsInfoMapper.insertBusSaleGoodsInfo(b);
            //扣库存
            if("1".equals(busSalesSlip.getInputStatus())){
                BusSkuStock busSkuStock = new BusSkuStock();
                busSkuStock.setSkuId(b.getGoodsNo());
                busSkuStock.setWarehouseNo(b.getWarehouseNo());
                busSkuStock.setBatchNo(b.getBatchNo());
                busSkuStock.setAmount(new BigDecimal(b.getQuantity()).negate());
                busSkuStock.setCanSaleAmount(new BigDecimal(b.getQuantity()).negate());
                busSkuStockList.add(busSkuStock);
            }
        }
        if(!CollectionUtils.isEmpty(busSaleGoodsInfoList)){
            iBusSkuStockService.insertOrUpdateSkuStock(busSkuStockList);
        }
        return 1;
    }
    @Autowired
    private IBusSkuStockService iBusSkuStockService;
    /**
     * 修改商品销售单
     *
     * @param busSalesSlipPage 商品销售单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusSalesSlip(BusSalesSlipPage busSalesSlipPage) {
        BusSalesSlip busSalesSlip = busSalesSlipPage.getBusSalesSlip();
        busSalesSlip.setUpdateTime(DateUtils.getNowDate());
        busSalesSlipMapper.updateBusSalesSlip(busSalesSlip);

        busSaleGoodsInfoMapper.deleteBusSaleGoodsInfoBySaleId(busSalesSlip.getId());
        List<BusSaleGoodsInfo> busSaleGoodsInfoList = busSalesSlipPage.getBusSaleGoodsInfoList();
        //出库库存
        List<BusSkuStock> busSkuStockList = new ArrayList<>();
        for (BusSaleGoodsInfo b : busSaleGoodsInfoList) {
            b.setSalesId(busSalesSlip.getId());
            busSaleGoodsInfoMapper.insertBusSaleGoodsInfo(b);

            //扣库存
            if("1".equals(busSalesSlip.getInputStatus())){
                BusSkuStock busSkuStock = new BusSkuStock();
                busSkuStock.setSkuId(b.getGoodsNo());
                busSkuStock.setWarehouseNo(b.getWarehouseNo());
                busSkuStock.setBatchNo(b.getBatchNo());
                busSkuStock.setAmount(b.getAmount().negate());
                busSkuStock.setCanSaleAmount(b.getAmount().negate());
                busSkuStockList.add(busSkuStock);
            }
        }
        if(!CollectionUtils.isEmpty(busSaleGoodsInfoList)){
            iBusSkuStockService.insertOrUpdateSkuStock(busSkuStockList);
        }
        return 1;
    }

    /**
     * 批量删除商品销售单
     *
     * @param ids 需要删除的商品销售单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusSalesSlipByIds(String[] ids) {
        busSalesSlipMapper.deleteBusSalesSlipByIds(ids);
        for (String s : ids) {
            busSaleGoodsInfoMapper.deleteBusSaleGoodsInfoBySaleId(s);
        }
        return 1;
    }

    /**
     * 删除商品销售单信息
     *
     * @param id 商品销售单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusSalesSlipById(String id) {
        busSalesSlipMapper.deleteBusSalesSlipById(id);
        busSaleGoodsInfoMapper.deleteBusSaleGoodsInfoBySaleId(id);
        return 1;
    }
}
